<template>
  <div>
    <form>
      <input type="text" value="" v-model="name" placeholder="请输入用户名">
      <input type="text" value="" v-model="age" placeholder="请输入年龄">
      <input type="file" @change="getFile($event)">
      <button @click.prevent="submitForm()">提交</button>
    </form>
  </div>
</template>

<script type="text/ecmascript-6">
import axios from 'axios'
export default {
  name: 'form-test',
  data () {
    return {
      name: '',
      age: '',
      file: ''
    }
  },
  methods: {
    getFile (event) {
      this.file = event.target.files[0]
      console.log(this.file)
    },
    submitForm (event) {
      let formData = new FormData()
      formData.append('name', this.name)
      formData.append('age', this.age)
      formData.append('file', this.file)
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }
      axios.post('/upload', formData, config).then(function (res) {
        if (res.status === 200) {
        }
      }).catch((e) => {
        console.log(e)
      }).finally(() => {
        console.log('finally')
      })
    }
  }
}
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>

</style>
